package com.zky.gulimall.product.web;

import com.alibaba.fastjson.JSON;
import com.zky.common.constant.AuthServerConstant;
import com.zky.gulimall.product.entity.CategoryEntity;
import com.zky.gulimall.product.service.CategoryService;
import com.zky.gulimall.product.vo.CateLog2Vo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Description index
 * @Author 张开源
 * @Date 2022/10/20 22:30
 * @Version 1.0
 **/
@Controller
@Slf4j
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model,HttpSession session) {
        //查出一级分类
        log.warn("session:"+session.getAttribute(AuthServerConstant.LOGIN_USER));
        List<CategoryEntity> categoryEntities=categoryService.getLevel1Catorys();
       model.addAttribute("categorys",categoryEntities);
       log.info("indexPage"+ JSON.toJSONString(categoryEntities));
       return "index";
    }

    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String,List<CateLog2Vo>> getCatalogJson(HttpSession session){
        log.warn("session:"+session.getAttribute(AuthServerConstant.LOGIN_USER));
        Map<String, List<CateLog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
}
